package oauth.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.BatchSize;
import org.hibernate.validator.constraints.Email;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.HashSet;
import java.util.Set;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class SysUser extends AbstractAuditingEntity
{
	@Id
	@GeneratedValue
	private Long id;

	@NotNull
	@Size(min = 1, max = 50)
	@Column(length = 50, unique = true, nullable = false)
	private String username;

	@JsonIgnore
	@NotNull
	@Size(min = 60, max = 60)
	@Column(length = 60)
	private String password;

	@Size(max = 50)
	@Column(length = 50)
	private String firstName;

	@Size(max = 50)
	@Column(length = 50)
	private String lastName;

	@Email
	@Size(min = 5, max = 100)
	@Column(length = 100, unique = true)
	private String email;

	@Size(max = 256)
	@Column(name = "image_url", length = 256)
	private String imageUrl;

	@JsonIgnore
	@ManyToMany(targetEntity = SysRole.class, fetch = FetchType.EAGER)
	@BatchSize(size = 20)
	private Set<SysRole> roles = new HashSet<>();

	@Transient
	private Set<GrantedAuthority> authorities = new HashSet<>();

	public Set<GrantedAuthority> getAuthorities()
	{
		Set<GrantedAuthority> userAuthotities = new HashSet<>();
		for (SysRole role : this.roles)
		{
			for (SysAuthority authority : role.getAuthorities())
			{
				userAuthotities.add(new SimpleGrantedAuthority(authority.getValue()));
			}
		}

		return userAuthotities;
	}

	public void setAuthorities(Set<GrantedAuthority> authorities)
	{
		this.authorities = authorities;
	}

	public Long getId()
	{
		return id;
	}

	public void setId(Long id)
	{
		this.id = id;
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public String getFirstName()
	{
		return firstName;
	}

	public void setFirstName(String firstName)
	{
		this.firstName = firstName;
	}

	public String getLastName()
	{
		return lastName;
	}

	public void setLastName(String lastName)
	{
		this.lastName = lastName;
	}

	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public String getImageUrl()
	{
		return imageUrl;
	}

	public void setImageUrl(String imageUrl)
	{
		this.imageUrl = imageUrl;
	}

	public Set<SysRole> getRoles()
	{
		return roles;
	}

	public void setRoles(Set<SysRole> roles)
	{
		this.roles = roles;
	}
}
